import Vue from 'vue';
import Router from 'vue-router';
import NProgress from 'nprogress';// 页面加载进度条
Vue.use(Router);
NProgress.configure({showSpinner: false});

/**
 * @note 基础路由表
 * */
export const constantRoutes = [
    // 手机校验
    {
        path: '/authentication',
        meta:{
                title:'手机校验'
        },
        component: () => import('@/views/authentication'),
    },
    // 登记
    {
        path: '/checkIn',
        meta:{
                title:'登记'
        },
        component: () => import('@/views/checkIn'),
        children:[{
            path:'selectProject',
            meta:{
                    title:'项目选择'
            },
            component: () => import('@/views/selectProject'),
        }]
    },
    // 登记
    {
        path: '/checkIn2',
        meta:{
                title:'登记'
        },
        component: () => import('@/views/checkIn2')
    },
    // 映射到认证页
    {
        path:'/',
        redirect:'/authentication'
    },
    {
        path:'/404',
        meta:{
            title:'404'
        },
        component: () => import('@/views/page404'),
    },
    // 404 配置
    {
        path: '*',
        redirect: '/404'
    }
];

// 创建路由对象
const createRouter = () => new Router({
    // 路由模式 hash
    scrollBehavior: () => ({
        y: 0
    }),
    routes: constantRoutes
});

// 路由对象实例化
const router = createRouter();

// 全局前置导航守卫
router.beforeEach((to, from, next) => {
    NProgress.start();
    document.title = to.meta.title;
    next();
});

// 全局后置导航守卫
router.afterEach((to,from) => {
    NProgress.done();
});

// 路由对象重定向
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router;